package QianFeng10.UDPDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * 客户端：
 * byte[] b = new byte[1024];//接收内容
 * DatagramPacket dp = new DatagramPacket(b, b.length);//接收内容
 * //客户端在5000端口等待
 * DatagramSocket client = new DatagramSocket(5000);
 * Client.receive(dp); //接收内容
 * System.out.printIn(new String(dp.getData(), 0, dp.getLength()));
 * client.close();
 **/
public class UDPClient {
    public static void main(String[] args) {
        byte[] bytes = new byte[1024];//接收内容
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        try {
            DatagramSocket socket = new DatagramSocket(8000);
            System.out.println("正在接收数据中...");
            socket.receive(dp);//接收内容
            String s = new String(dp.getData(), dp.getOffset(), dp.getLength());
            System.out.println(s);
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
